package com.xmy.cultivate.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.xmy.cultivate.entity.StaffRanking;
import com.xmy.cultivate.mapper.StaffRankingMapper;
import com.xmy.cultivate.service.IStaffRankingService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.Objects;

/**
 * <p>
 * 员工排位名称 服务实现类
 * </p>
 *
 * @author hpiggy
 * @since 2023-05-16
 */
@Service
public class StaffRankingServiceImpl extends ServiceImpl<StaffRankingMapper, StaffRanking> implements IStaffRankingService {

    @Autowired
    StaffRankingMapper staffRankingMapper;

    /**
     * 第一个排位编号 旧的
     *
     * @param reMap
     * @return
     */
    @Override
    public Long getId1(Map reMap) {
        QueryWrapper rank1Wrapper = new QueryWrapper<>();
        String year = (String) reMap.get("year");
        String staffRankingName = (String) reMap.get("staffRankingName");
        rank1Wrapper.eq("deleted", 0);
        rank1Wrapper.like(StringUtils.isNotBlank(staffRankingName), "name", staffRankingName);
        rank1Wrapper.eq(StringUtils.isNotBlank(year), "DATE_FORMAT(staff_ranking.ranking_date,'%Y')", year);
        rank1Wrapper.orderByDesc("ranking_date");
        if (StringUtils.isBlank(staffRankingName) && StringUtils.isBlank(year)) {
            rank1Wrapper.last("LIMIT 1 OFFSET 1");
        } else {
            rank1Wrapper.last("LIMIT 1");
        }
        Long rankId1 = staffRankingMapper.getId(rank1Wrapper);
        if (rankId1 == null) {
            rank1Wrapper.clear();
            rank1Wrapper.eq("deleted", 0);
            rank1Wrapper.last("LIMIT 1 OFFSET 1");
            rankId1 = staffRankingMapper.getId(rank1Wrapper);
        }
        return rankId1;
    }

    /**
     * 第二个排位 最新的
     *
     * @param reMap
     * @return
     */
    @Override
    public Long getId2(Map reMap) {
        QueryWrapper rank2Wrapper = new QueryWrapper<>();
        String year2 = (String) reMap.get("year2");
        String staffRankingName2 = (String) reMap.get("staffRankingName2");
        rank2Wrapper.eq("deleted", 0);
        rank2Wrapper.like(StringUtils.isNotBlank(staffRankingName2), "name", staffRankingName2);
        rank2Wrapper.eq(StringUtils.isNotBlank(year2), "DATE_FORMAT(staff_ranking.ranking_date,'%Y')", year2);
        rank2Wrapper.orderByDesc("ranking_date");
        rank2Wrapper.last("LIMIT 1");
        Long rankId2 = staffRankingMapper.getId(rank2Wrapper);
        if (rankId2 == null) {
            rank2Wrapper.clear();
            rank2Wrapper.eq("deleted", 0);
            rank2Wrapper.last("LIMIT 1");
            rankId2 = staffRankingMapper.getId(rank2Wrapper);
        }
        return rankId2;
    }

    /**
     * 删除排位
     * @param id
     * @return
     */
    @Override
    public Boolean removeStaffRankingById(String id) {
        StaffRanking staffRanking = staffRankingMapper.selectById(id);
        if (Objects.isNull(staffRanking)) {
            return false;
        }
        staffRankingMapper.deleteById(id);
        return true;
    }


    /**
     * 查询排位编号
     *
     * @param wrapper
     * @return
     */
    @Override
    public Long getId(Wrapper wrapper) {
        return staffRankingMapper.getId(wrapper);
    }
}
